python cartesian product

34

import itertools

somelists = [
   [1, 2, 3],
   ['a', 'b'],
   [4, 5]
]
for element in itertools.product(*somelists):
    print(element)
# initialize lists
listA = [1, 4, 6, 7]
listB = [1, 3]
  
# Construct Cartesian Product Tuple list
# using list comprehension
res = [(a, b) for a in listA for b in listB]
  
# printing result
print("The Cartesian Product is : " + str(res))
[ (x, y) for x in xs for y in ys ]
a = [1, 2, 3]
b = ["a", "b", "c"]

index = pd.MultiIndex.from_product([a, b], names = ["a", "b"])

pd.DataFrame(index = index).reset_index()

Comments

Submit
0 Comments